package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmj.sy.mom.srv.aps.bean.entity.base.SensPermission;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.senspermission.SensPermissionEditReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.senspermission.SensPermissionReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.senspermission.SensPermissionResVo;
import com.zmj.sy.mom.srv.aps.mapper.SensPermissionMapper;
import com.zmj.sy.mom.srv.aps.utils.SySecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@RequiredArgsConstructor
@Service
public class SensPermissionService {

    private final SensPermissionMapper sensPermissionMapper;

    public BaseListResVo<SensPermissionResVo> getSensPermission(SensPermissionReqVo reqVo) {
        LambdaQueryWrapper<SensPermission> qw = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(reqVo.getLocation())) {
            qw.like(SensPermission::getLocation, reqVo.getLocation());
        }
        if (StringUtils.hasText(reqVo.getMyCode())) {
            qw.like(SensPermission::getMyCode, reqVo.getMyCode());
        }
        if (StringUtils.hasText(reqVo.getName())) {
            qw.like(SensPermission::getName, reqVo.getName());
        }
        if (StringUtils.hasText(reqVo.getUserCode())) {
            qw.like(SensPermission::getUserCode, reqVo.getUserCode());
        }
        if (StringUtils.hasText(reqVo.getRemark())) {
            qw.like(SensPermission::getRemark, reqVo.getRemark());
        }
        qw.orderByDesc(SensPermission::getSeq);
        List<SensPermission> list = sensPermissionMapper.selectList(qw);
        return BaseListResVo.of(list, SensPermissionResVo.class);
    }

    public void editSensPermission(SensPermissionEditReqVo reqVo) {
        String userCode = SySecurityUtils.getUserCode();
        sensPermissionMapper.lambdaUpdate()
                .eq(SensPermission::getId, reqVo.getId())
                .set(SensPermission::getUserCode, reqVo.getUserCode())
                .set(SensPermission::getModifyDateTime, LocalDateTime.now())
                .set(SensPermission::getModifyUsername, userCode)
                .set(StringUtils.hasText(reqVo.getRemark()), SensPermission::getRemark, reqVo.getRemark())
                .update();
    }

}
